#pragma once
#include<muduo/net/Buffer.h>
#include<memory>

class BaseBuffer
{
public:
    using ptr = std::shared_ptr<BaseBuffer>;
    BaseBuffer() {}
    virtual ~BaseBuffer() {}

    virtual size_t readableBytes() = 0;
    virtual int32_t peekInt32() = 0;
    virtual void retrieveInt32() = 0;
    virtual int32_t readInt32() = 0;
    virtual std::string retrieveAsString(size_t len) = 0;
};

class MuduoBuffer : public BaseBuffer
{
public:
    using ptr = std::shared_ptr<MuduoBuffer>;
    MuduoBuffer(muduo::net::Buffer* buf) : _pbuf(buf) {}
    ~MuduoBuffer() {}

    // 4字节 硬件适配性和跨平台兼容性：操作宽度，内存对齐，指令效率
    virtual size_t readableBytes() override {return _pbuf->readableBytes();}
    virtual int32_t peekInt32() override {return _pbuf->peekInt32();}
    virtual void retrieveInt32() override {_pbuf->retrieveInt32();}
    virtual int32_t readInt32() override {return _pbuf->readInt32();}
    virtual std::string retrieveAsString(size_t len) override {return _pbuf->retrieveAsString(len);}
private:
    muduo::net::Buffer* _pbuf;
};

class BufferFactory
{
public:
    template<typename T, typename ...Args>
    static BaseBuffer::ptr creat(Args&& ...args)
    {
        return std::make_shared<T>(std::forward<Args>(args)...);
    }
};